package service

import (
	"fmt"
	"time"

	"gitee.com/bluesky2006/alarm/dao"
)

// AlarmResult Model
type AlarmResult struct {
	ID                int       `json:"id"`
	AlarmSubject      string    `json:"alarmSubject"`
	SystemType        string    `json:"systemType"`
	SystemTypeDesc    string    `json:"systemTypeDesc"`
	AlarmCode         string    `json:"alarmCode"`
	Severity          int       `json:"severity"`
	Status            int       `json:"status"`
	AlarmDefinitionID int       `json:"alarmDefinitionId"`
	AlarmTime         time.Time `json:"alarmTime"`
	Duration          string    `json:"duration"`
	UpdatedTime       time.Time `json:"updatedTime"`
	Count             int       `json:"count"`
	CreatedBy         string    `json:"createdBy"`
	UpdatedBy         string    `json:"updatedBy"`
}

var sqlstr = `select ai.id,ai.alarm_definition_id,ai.alarm_subject,ai.system_type,service.label system_type_desc,
ai.severity,concat(ai.master_code,'-',ai.sub_code) alarm_code,ai.first_time , ai.status,
ai.updated_time, ai.created_time alarm_time,TIMESTAMPDIFF(SECOND, ai.first_time, now()) duration,ai.count,created_by,updated_by from (
-- -----
select alarm.*,b.master_code,b.sub_code,b.system_type,b.severity,b.alarm_subject 
from 
(
select a.*,b.count from alarm_record a right join (
select alarm_definition_id,max(updated_time) updated_time,count(1) count from alarm_record group by alarm_definition_id)b
on a.updated_time = b.updated_time and a.alarm_definition_id = b.alarm_definition_id
) alarm left join 
alarm_definition b on alarm.alarm_definition_id = b.id 
-- -----
) ai left join
(select * from dict_detail where dict_id = (select id from dict where name = 'system_type'))  service 
on ai.system_type = service.value  order by updated_time desc`

// LoadActiveAlarmList load active alarms from alarm_record table
func LoadActiveAlarmList(systemType int) (alarmList []*AlarmResult, err error) {

	sql := fmt.Sprintf("select * from (%s) a where system_type = %v ", sqlstr, systemType)
	if err = dao.DB.Debug().Raw(sql).Scan(&alarmList).Error; err != nil {
		fmt.Println("limit-->", err)
		return nil, err
	}
	return alarmList, nil
}

// LoadActiveAlarms load active alarms from alarm_record table
func LoadActiveAlarms(systemType int) (alarmList []AlarmResult, err error) {

	sql := fmt.Sprintf("select * from (%s) a where system_type = %v ", sqlstr, systemType)
	if err = dao.DB.Debug().Raw(sql).Scan(&alarmList).Error; err != nil {
		fmt.Println("limit-->", err)
		return nil, err
	}
	return alarmList, nil
}

// LoadActiveAlarmRecords load active alarms from alarm_record table
func LoadActiveAlarmRecords(params map[string]string) (alarmList []AlarmResult, err error) {
	fmt.Printf("params:%#v\n", params)
	condition := " 1 = 1 "
	var num string = "100"
	for k, v := range params {
		fmt.Println(k, v)
		if k == "severity" {
			if v != "" {
				condition = fmt.Sprintf("%s and severity in(%s)", condition, v)
			}
		}
		if k == "alarmCode" {
			if v != "" {
				condition = fmt.Sprintf("%s and alarm_code like'%s%%'", condition, v)
			}
		}
		if k == "systemType" {
			if v != "" {
				condition = fmt.Sprintf("%s and system_type_desc like'%s%%'", condition, v)
			}
		}
		if k == "alarmSubject" {
			if v != "" {
				condition = fmt.Sprintf("%s and alarm_subject like'%s%%'", condition, v)
			}
		}
		if k == "num" {
			if v != "" {
				num = v
			}
		}
	}

	sql := fmt.Sprintf("select * from (%s) a where %s limit 0,%s", sqlstr, condition, num)
	//fmt.Println("sql======>", sql)
	if err = dao.DB.Debug().Raw(sql).Scan(&alarmList).Error; err != nil {
		fmt.Println("limit-->", err)
		return nil, err
	}
	return alarmList, nil
}
